package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.bairong.BairongData;
import com.ucredit.babel.common.model.third.bairong.BairongLimitConsumptionRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 百融限高服务
 *
 * @author renzhanzheng
 */

public class BairongLimitConsumptionParse extends ParseBase{
    private Text val = new Text();
    private Text key = new Text();

    /**
     *
     * 百融限高服务
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     * @return
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {;
        BairongLimitConsumptionRecord obj = (BairongLimitConsumptionRecord)model;
        BairongData data = obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());

        StringBuilder sb = getBaseInfo(obj, successful);
        //返回码
        sb.append(info.getString("code") == null ? "" : info.getString("code")).append(CRT001);
        //流水号
        sb.append(info.getString("swift_number") == null ? "" : info.getString("swift_number")).append(CRT001);
        //策略编号
        sb.append(info.getString("rs_strategy_id") == null ? "" : info.getString("rs_strategy_id")).append(CRT001);
        //策略使用事件类型
        sb.append(info.getString("rs_scene") == null ? "" : info.getString("rs_scene")).append(CRT001);
        //是否命中百融限高规则
        sb.append(info.getString("flag_ruleexecutionlimited") == null ? "" : info.getString("flag_ruleexecutionlimited")).append(CRT001);
        //是否命中百融限高
        sb.append(info.getString("flag_executionlimited") == null ? "" : info.getString("flag_executionlimited")).append(CRT001);
        //策略产品类型
        sb.append(info.getString("rs_product_type") == null ? "" : info.getString("rs_product_type")).append(CRT001);
        //规则集决策权重
        sb.append(info.getString("rs_Rule_weight") == null ? "" : info.getString("rs_Rule_weight")).append(CRT001);
        //策略使用平台
        sb.append(info.getString("rs_platform") == null ? "" : info.getString("rs_platform")).append(CRT001);
        //最终决策结果
        sb.append(info.getString("rs_final_decision") == null ? "" : info.getString("rs_final_decision")).append(CRT001);
        //规则集决策结果
        sb.append(info.getString("rs_Rule_decision") == null ? "" : info.getString("rs_Rule_decision")).append(CRT001);
        //策略引擎产品输出标识
        sb.append(info.getString("flag_riskstrategy") == null ? "" : info.getString("flag_riskstrategy")).append(CRT001);
        //规则权重
        sb.append(info.getString("Rule_final_weight") == null ? "" : info.getString("Rule_final_weight")).append(CRT001);
        //策略产品名称
        sb.append(info.getString("rs_product_name") == null ? "" : info.getString("rs_product_name")).append(CRT001);
        //策略版本
        sb.append(info.getString("rs_strategy_version") == null ? "" : info.getString("rs_strategy_version")).append(CRT001);
        //规则结果
        sb.append(info.getString("Rule_final_decision") == null ? "" : info.getString("Rule_final_decision")).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_bairong_limitconsumption_inc";
    }

    @Override
    public String getType() {
        return RecordType.BAIRONG_LIMITCONSUMPTION.toString();
    }

    @Override
    public String getComment() {
        return "百融限高服务";
    }
}
